#pragma once
#include<iostream>
#include<assert.h>
#include<string.h>

namespace wjb
{
	class string
	{
	public:
		typedef char* iterator;



		string(const char* str = "")
			:_size(strlen(str)),
			 _capacity(_size)
		{
			if (_size == 0)
			{
				_str = new char[1];
			}

			else
				_str = new char[_capacity + 1];
				for (size_t i = 0; i <= _size; i++)
				{
					_str[i] = str[i];
				}

		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

		const char* c_str()const
		{
			return _str;
		}

		char& operator[](size_t pos)
		{
			return _str[pos];
		}
		const char& operator[](size_t pos)const
		{
			return _str[pos];
		}

		size_t size()
		{
			return _size;
		}

		char* begin()
		{
			return _str;
		}
		char* end()
		{
			return _str + _size;
		}
		const char* begin()const
		{
			return _str;
		}
		const char* end()const
		{
			return _str + _size;
		}

		void pushback(char ch)
		{
			if (_size == 0)
			{
				reserve(1);
			}
			if (_size == _capacity)
			{
				reserve(_capacity * 2);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';
		}
		void append(const char* str)
		{
			int len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
				for (size_t i = 0; i <= len; i++)
				{
					_str[_size + i] = str[i];
				}
			}
			_size += len;

		}


		string& operator+=(char ch)
		{
			pushback(ch);
			return *this;
		}


		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				tmp = nullptr;
				_capacity = n;
			}
		}

		void insert(size_t pos,size_t n,char ch)
		{
			assert(pos < _size);
			if (_size + n > _capacity)
			{
				reserve(_size + n);
			}
			int end = _size;
			while (end >= pos && end != npos)
			{
				_str[end + n] = _str[end];
				end--;
			}
			for (size_t i = 0; i < n; i++)
			{
				_str[pos + i] = ch;
			}
			_size += n;
		}
		void insert(size_t pos, const char* str)
		{
			assert(pos < _size);
			int len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			int end = _size;
			while (end >= pos && end != npos)
			{
				_str[end + len] = _str[end];
				--end;
			}
			for (size_t i = 0; i < len; i++)
			{
				_str[pos + i] = str[i];
			}
			_size += len;
		}

		void erase(size_t pos = 0, size_t len = npos)
		{
			assert(pos < _size);
			if (pos + len >= _size || len == npos)
			{
				_str[pos] = '\0';
			}
			else
			{
				int end = _size;
				while (end >= pos && pos+len <= _size)
				{
					_str[pos] = _str[pos + len];
					--end;
					++pos;
				}
			}
			_size -= len;
		}

		size_t find(char ch, size_t pos = 0)const
		{
			assert(pos < _size);
			for (size_t i = 0; i+pos < _size; i++)
			{
				if (_str[i+pos] = ch)
				{
					return i;
				}
			}
			return npos;
		}

		size_t find(const char* str,size_t pos = 0)const
		{
			assert(pos < _size);
			const char* tmp = strstr(_str + pos, str);
			if (tmp == nullptr)
			{
				return npos;
			}
			return tmp - _str;
		}

		string(const string& s)
		{
			_str = new char[s._capacity + 1];
			_size = s._size;
			_capacity = s._capacity;
			for (size_t i = 0; i <= _size; i++)
			{
				_str[i] = s._str[i];
			}
		}

		string substr(size_t pos = 0 ,size_t len = npos)const
		{
			assert(pos < _size);
			string tmp;
			
			for (size_t i = 0; i < len && i+pos < _size; i++)
			{
				tmp += _str[pos+i];
			}
			return tmp;
		}

	private:
		int _size;
		int _capacity;
		char* _str;
	public:
		static size_t npos;
	};
	size_t string::npos = -1;
}